<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="url">
      <el-input v-model="dataForm.url" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="原网页地址" prop="pageUrl">
      <el-input v-model="dataForm.pageUrl" placeholder="原网页地址"></el-input>
    </el-form-item>
    <el-form-item label="" prop="name">
      <el-input v-model="dataForm.name" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="avatar">
      <el-input v-model="dataForm.avatar" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="平台" prop="platform">
      <el-input v-model="dataForm.platform" placeholder="平台"></el-input>
    </el-form-item>
    <el-form-item label="具体来源" prop="shop">
      <el-input v-model="dataForm.shop" placeholder="具体来源"></el-input>
    </el-form-item>
    <el-form-item label="" prop="goods">
      <el-input v-model="dataForm.goods" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="资源类型：图片、视频" prop="resType">
      <el-input v-model="dataForm.resType" placeholder="资源类型：图片、视频"></el-input>
    </el-form-item>
    <el-form-item label="" prop="remark">
      <el-input v-model="dataForm.remark" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="des">
      <el-input v-model="dataForm.des" placeholder="描述"></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="权重" prop="weight">
      <el-input v-model="dataForm.weight" placeholder="权重"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          url: '',
          pageUrl: '',
          name: '',
          avatar: '',
          platform: '',
          shop: '',
          goods: '',
          resType: '',
          remark: '',
          des: '',
          createTime: '',
          weight: ''
        },
        dataRule: {
          url: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          pageUrl: [
            { required: true, message: '原网页地址不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          avatar: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          platform: [
            { required: true, message: '平台不能为空', trigger: 'blur' }
          ],
          shop: [
            { required: true, message: '具体来源不能为空', trigger: 'blur' }
          ],
          goods: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          resType: [
            { required: true, message: '资源类型：图片、视频不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          des: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          weight: [
            { required: true, message: '权重不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/showres/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.url = data.showres.url
                this.dataForm.pageUrl = data.showres.pageUrl
                this.dataForm.name = data.showres.name
                this.dataForm.avatar = data.showres.avatar
                this.dataForm.platform = data.showres.platform
                this.dataForm.shop = data.showres.shop
                this.dataForm.goods = data.showres.goods
                this.dataForm.resType = data.showres.resType
                this.dataForm.remark = data.showres.remark
                this.dataForm.des = data.showres.des
                this.dataForm.createTime = data.showres.createTime
                this.dataForm.weight = data.showres.weight
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/showres/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'url': this.dataForm.url,
                'pageUrl': this.dataForm.pageUrl,
                'name': this.dataForm.name,
                'avatar': this.dataForm.avatar,
                'platform': this.dataForm.platform,
                'shop': this.dataForm.shop,
                'goods': this.dataForm.goods,
                'resType': this.dataForm.resType,
                'remark': this.dataForm.remark,
                'des': this.dataForm.des,
                'createTime': this.dataForm.createTime,
                'weight': this.dataForm.weight
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
